package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<NursingLevelVo> findAll() {
        //调用方法返回实体类
        List<NursingLevel> nursingLevels = nursingLevelMapper.findAll();
        //转为VO
        List<NursingLevelVo> nursingLevelVos = new ArrayList<>();
        for (NursingLevel nursingLevel : nursingLevels) {
            BeanUtils.copyProperties(nursingLevels,nursingLevelVos);
        }
        return nursingLevelVos;
    }

    /**
     * 新增等级
     * @param nursingLevelDto
     */
    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevelMapper.add(nursingLevel);
    }

    /**
     * 分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingLevelVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        //分页查询组件
        PageHelper.startPage(pageNum,pageSize);
        //调用方法
        Page<NursingLevel> nursingLevels = nursingLevelMapper.selectPage(name, status);
        //转为VO
        PageResponse<NursingLevelVo> pageResponse = PageResponse.of(nursingLevels, NursingLevelVo.class);
        return pageResponse;
    }

    @Override
    public NursingLevelVo findById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.findById(id);
        NursingLevelVo nursingLevelVo= BeanUtil.toBean(nursingLevel,NursingLevelVo.class);
        return nursingLevelVo;
    }

    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setCreateTime(LocalDateTime.now());
        BeanUtils.copyProperties(nursingLevelDto,nursingLevel);
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void deleteById(Long id) {
        nursingLevelMapper.deleteById(id);
    }

    @Override
    public void status(Long id, Integer status) {
        nursingLevelMapper.status(id,status);
    }
}
